package com.mapull;

import java.util.HashMap;
import java.util.Map;

public class MapullApplication {
    public static void main(String[] args) {
        int[] nums ={11,0,23,10,41,14,50};
        String[] arr = {"h","e","l","l","o"};
//        twoSum(nums, 64);
//        reverse(arr);
//        System.out.println(myAtoi("   -1233498jakj34"));
        System.out.println(countAndSay(15));
    }

    public static String countAndSay(int n) {
        if(n == 1){
            return "1";
        }
        String str = countAndSay(n - 1);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        char c = str.charAt(0);
        for(int i = 0; i<str.length(); i++){
            if(c == str.charAt(i)){
                count++;
            }else{
                sb.append(count).append(c);
                count = 1;
                c = str.charAt(i);
            }
        }
        sb.append(count).append(c);
        return sb.toString();
    }

    public int strStr(String haystack, String needle) {
        int res  =0;
        if("".equals(haystack) || "".equals(needle)){
            return 0;
        }
        if(haystack.length() < needle.length()){
            return res;
        }
        char[] arr = haystack.toCharArray();
        char[] needs = needle.toCharArray();
        for(int i =0; i< arr.length; i++){
            if(arr[i] == needs[0]){
                for(int j = 0; j < needs.length; j++){
                    if(needs[i] != arr[i+j]){
                        break;
                    }
                    if(j == needs.length-1){
                        return i;
                    }
                }
            }
        }
        return res;

    }
    public static int myAtoi(String s) {
        s = s.trim();
        char c = s.charAt(0);
        boolean negative = c == '-';
        char[] arr = s.toCharArray();
        int res =0;
        for(int i = 0; i< arr.length; i++){
            if( i == 0 && negative){
                continue;
            }
            if(Character.isDigit(arr[i])){
                if(res*10/10 != res){
                    return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                }
                res = res*10 + Integer.valueOf(arr[i] - '0');
            }else{
                System.out.println(negative);
                return negative ? res*-1: res;
            }
        }
        return res;
    }
    public static void reverse(String[] arr){
        int last = arr.length -1;
        for(int i = 0; i < arr.length/2; i++){
            String tmp = arr[i];
            arr[i] = arr[last];
            arr[last] = tmp;
            last--;
        }

    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i =0; i< nums.length; i++){
            System.out.println(map);
            Integer tmp =map.get(target - nums[i]);
            if(tmp != null){
                return new int[]{tmp, i};
            }
            map.put(nums[i], i);
        }
        return new int[]{0, 0};
    }

}
